@echo off
setlocal enabledelayedexpansion
::call creattls.bat
::call filetobin.bat
SET BASE_PATH=bin
SET PACKAGE_PATH=package
SET TEMP_PATH_C=ngrok
SET TEMP_PATH_S=ngrokd
SET SERVER_PATH=%BASE_PATH%
::\server
SET CLIENT_PATH=%BASE_PATH%
::\client
md %BASE_PATH%
md %SERVER_PATH%
md %CLIENT_PATH%
md %PACKAGE_PATH%
md %TEMP_PATH_C%
md %TEMP_PATH_S%
::darwin	386
::darwin	amd64
::freebsd	386
::freebsd	amd64
::linux	386
::linux	amd64
::linux	arm
::windows	386
::windows	amd64

::初始化数组
SET Obj_Length=9

SET Obj[0].GOOS=windows
SET Obj[0].GOARCH=amd64
SET Obj[0].ENABLE=1

SET Obj[1].GOOS=windows
SET Obj[1].GOARCH=386
SET Obj[1].ENABLE=0

SET Obj[2].GOOS=darwin
SET Obj[2].GOARCH=386
SET Obj[2].ENABLE=0

SET Obj[3].GOOS=darwin
SET Obj[3].GOARCH=amd64
SET Obj[3].ENABLE=0

SET Obj[4].GOOS=freebsd
SET Obj[4].GOARCH=386
SET Obj[4].ENABLE=0

SET Obj[5].GOOS=freebsd
SET Obj[5].GOARCH=amd64
SET Obj[5].ENABLE=1

SET Obj[6].GOOS=linux
SET Obj[6].GOARCH=386
SET Obj[6].ENABLE=0

SET Obj[7].GOOS=linux
SET Obj[7].GOARCH=amd64
SET Obj[7].ENABLE=1

SET Obj[8].GOOS=linux
SET Obj[8].GOARCH=arm
SET Obj[8].ENABLE=0

SET Obj_Index=0

GOTO LoopStart

::数组循环
:LoopStart
IF %Obj_Index% EQU %Obj_Length% GOTO :END
 
SET Obj_Current.GOOS=
SET Obj_Current.GOARCH=
SET Obj_Current.ENABLE=0
 
FOR /F "usebackq delims==. tokens=1-3" %%I IN (`SET Obj[%Obj_Index%]`) DO (
  SET Obj_Current.%%J=%%K
)

if %Obj_Current.ENABLE%==1 (
    ::设置编译条件
    SET CGO_ENABLED=0
    SET GOOS=!Obj_Current.GOOS!
    SET GOARCH=!Obj_Current.GOARCH!

    ECHO building====%Obj_Index%====!GOOS!====!GOARCH!======================
    if !GOOS!==windows (
        SET ISEXE=.exe
        SET RUNFILE=.bat
        SET ZIPSUFFIX=.zip
    )else (
        SET ISEXE=
        SET RUNFILE=.sh
        SET ZIPSUFFIX=.gz
    )
    ::编译文件
    go build -tags 'release' -o %BASE_PATH%\!GOOS!_!GOARCH!_ngrok!ISEXE! gitee.com/hailaz/ngrok/main/ngrok
    go build -tags 'release' -o %BASE_PATH%\!GOOS!_!GOARCH!_ngrokd!ISEXE! gitee.com/hailaz/ngrok/main/ngrokd
    ::删除临时文件
    del /Q %TEMP_PATH_C%\*
    del /Q %TEMP_PATH_S%\*
    ::复制临时文件
    ::客户端
    copy %BASE_PATH%\!GOOS!_!GOARCH!_ngrok!ISEXE! %TEMP_PATH_C%\ngrok!ISEXE!
    copy %BASE_PATH%\client\ngrok.cfg %TEMP_PATH_C%\ngrok.cfg
    copy %BASE_PATH%\client\client!RUNFILE! %TEMP_PATH_C%\client!RUNFILE!
    ::服务端
    copy %BASE_PATH%\!GOOS!_!GOARCH!_ngrokd!ISEXE! %TEMP_PATH_S%\ngrokd!ISEXE!
    ::压缩打包文件
    if !GOOS!==windows (
        ::客户端
        del %PACKAGE_PATH%\!GOOS!_!GOARCH!_ngrok!ZIPSUFFIX!
        7z.exe a -tzip %PACKAGE_PATH%\!GOOS!_!GOARCH!_ngrok!ZIPSUFFIX! "%TEMP_PATH_C%\*" -r -mx=9
        ::服务端
        del %PACKAGE_PATH%\!GOOS!_!GOARCH!_ngrokd!ZIPSUFFIX!
        7z.exe a -tzip %PACKAGE_PATH%\!GOOS!_!GOARCH!_ngrokd!ZIPSUFFIX! "%TEMP_PATH_S%\*" -r -mx=9
    )else (
        ::客户端
        7z.exe a -ttar %PACKAGE_PATH%\ngrok.tar "%TEMP_PATH_C%\*" -r
        del %PACKAGE_PATH%\!GOOS!_!GOARCH!_ngrok.tar!ZIPSUFFIX!
        7z.exe a -tgzip %PACKAGE_PATH%\!GOOS!_!GOARCH!_ngrok.tar!ZIPSUFFIX! %PACKAGE_PATH%\ngrok.tar -mx=9
        del %PACKAGE_PATH%\ngrok.tar
        ::服务端
        7z.exe a -ttar %PACKAGE_PATH%\ngrokd.tar "%TEMP_PATH_S%\*" -r
        del %PACKAGE_PATH%\!GOOS!_!GOARCH!_ngrokd.tar!ZIPSUFFIX!
        7z.exe a -tgzip %PACKAGE_PATH%\!GOOS!_!GOARCH!_ngrokd.tar!ZIPSUFFIX! %PACKAGE_PATH%\ngrokd.tar -mx=9
        del %PACKAGE_PATH%\ngrokd.tar
    )


    ECHO build finished
    ECHO.
)
 
SET /A Obj_Index=%Obj_Index% + 1
 
GOTO LoopStart


:END
rd /s /Q %TEMP_PATH_C%
rd /s /Q %TEMP_PATH_S%